//
//  HomeCollectionView.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/9/18.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit
import SDWebImage

class HomeCollectionView: UICollectionView {
    
    // MARK:- 属性
    var picUrls : [URL] = [URL]() {
        didSet {
            self.reloadData()
        }
    }
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        dataSource = self
        delegate = self
    }
    
}

// MARK:- collectionView的数据源方法
extension HomeCollectionView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homePicCell", for: indexPath) as! HomePicCell
        cell.picUrl = picUrls[indexPath.item];
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dic = [ShowPhotoBrowserIndexKey : indexPath, ShowPhotoBrowserUrlsKey : picUrls] as [String : Any]
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: ShowPhotoBrowserNote), object: self, userInfo: dic)
    }
}

extension HomeCollectionView : AnimatorPresentedDelegate {
    func startRect(indexPath: NSIndexPath) -> CGRect {
        //获取cell
        let cell = self.cellForItem(at: indexPath as IndexPath)!
        let startFrame = self.convert(cell.frame, to: UIApplication.shared.keyWindow)
        return startFrame
    }
    
    func endRect(indexPath: NSIndexPath) -> CGRect {
        let picUrl = picUrls[indexPath.item]
        let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: picUrl.absoluteString)
        
        //计算结束之后的frame
        let W = UIScreen.main.bounds.size.width
        let H = W / (image?.size.width)! * (image?.size.height)!
        var Y : CGFloat = 0
        if H > UIScreen.main.bounds.height {
            Y = 0
        } else {
            Y = (UIScreen.main.bounds.height - H) * 0.5
        }
        return CGRect(x: 0, y: Y, width: W, height: H)
    }
    
    func imageView(indexPath: NSIndexPath) -> UIImageView {
        let imageView = UIImageView()
        let picUrl = picUrls[indexPath.item]
        let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: picUrl.absoluteString)
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }
}

class HomePicCell: UICollectionViewCell {
    
    // MARK:- 定义模型属性
    var picUrl : URL? {
        didSet {
            guard let picUrl = picUrl else {
                return
            }
            
            picImgView.sd_setImage(with: picUrl, placeholderImage: UIImage(named: "empty_picture"), options: [], completed: nil)
        }
    }
    
    // MARK:- 控件属性
    @IBOutlet weak var picImgView: UIImageView!
}
